// Replication of Table 3


// load data
use "data/table3/fouirnaies_hall_financial_incumbency_advantage.dta", clear

// keep only US House and drop State legislatures
keep if statelev==0
rename year yr
quietly: tab yr, g(dyr)
label variable yr "Election Year"

// Define panel district structure
g state_dist=state+"_"+string(dist)
encode state_dist, g(state_dist_code)
xtset state_dist_code yr, delta(2)

// Drop if missing vote share
drop if rv==.
label variable rv "(Dem votes/(Dem votes+Rep votes))*100-50"

// Define Democrat dummy
drop dem
g dem=0 if rv!=.
replace dem=1 if rv>0 & rv!=.
g rv_dem = dem*rv

// Define Majorities
g demmaj = 1
replace demmaj = 0 if yr==1946
replace demmaj = 0 if yr==1952
replace demmaj = 0 if yr>=1994 & yr<=2004
replace demmaj = 0 if yr>=2010 & yr<=2014
label variable demmaj "Dem has national majority"

g maj = 0
replace maj = 1 if dem==1 & demmaj==1
replace maj = 1 if dem==0 & demmaj==0
label variable maj "District belongs to majority"

// Generate a Congress variable
g congress=0
replace congress=114 if yr==2014
replace congress=113 if yr==2012
replace congress=112 if yr==2010
replace congress=111 if yr==2008
replace congress=110 if yr==2006
replace congress=109 if yr==2004
replace congress=108 if yr==2002
replace congress=107 if yr==2000
replace congress=106 if yr==1998
replace congress=105 if yr==1996
replace congress=104 if yr==1994
replace congress=103 if yr==1992
replace congress=102 if yr==1990
replace congress=101 if yr==1988
replace congress=100 if yr==1986
replace congress=99 if yr==1984
replace congress=98 if yr==1982
replace congress=97 if yr==1980
replace congress=96 if yr==1978
replace congress=95 if yr==1976
replace congress=94 if yr==1974
replace congress=93 if yr==1972
replace congress=92 if yr==1970
replace congress=91 if yr==1968
replace congress=90 if yr==1966
replace congress=89 if yr==1964
replace congress=88 if yr==1962
replace congress=87 if yr==1960
replace congress=86 if yr==1958
replace congress=85 if yr==1956
replace congress=84 if yr==1954
replace congress=83 if yr==1952
replace congress=82 if yr==1950
replace congress=81 if yr==1948
replace congress=80 if yr==1946

// Outcomes
label variable dv_money_level_dem1 "Money to dem at t+1"
label variable dv_money_level_dem0 "Money to rep at t+1"


// To incumbent
g dv_money_inc = .
replace dv_money_inc = dv_money_level_dem1 if dem==1
replace dv_money_inc = dv_money_level_dem0 if dem==0
label variable dv_money_inc "Money to incumbent"

g dv_group_inc = .
replace dv_group_inc = dv_group_level_dem1 if dem==1
replace dv_group_inc = dv_group_level_dem0 if dem==0
label variable dv_group_inc "Investor money to incumbent"

g dv_nongroup_inc = dv_money_inc - dv_group_inc
label variable dv_nongroup_inc "Non-investor money to incumbent"




//Different samples
// 1992 last year of DemMaj
drop if yr==2006
g sample1=1 if yr<=1994
label variable sample1 "1978-1994"
tab demmaj if sample1==1
g sample2=1 if yr>=1990
label variable sample2 "1990-2004"
tab demmaj if sample2==1
g sample3=1 
label variable sample3 "1978-2004"
g sample4=1 if yr<=1992
label variable sample4 "Dem yrs: 1978-92"
g sample5=1 if yr>1992
label variable sample5 "Rep yrs: 1994-2004"

foreach sample in 1 2 3 {
tab demmaj if sample`sample'==1 & rv>-11 & rv<11
corr dem maj if sample`sample'==1 & rv>-11 & rv<11
}


// save intermediate data for figure in appendix
save "data/table3/fh_fia_inter.dta", replace

*********************************************************
********************************************
****ANALYSIS*********************************************
********************************************
*********************************************************



eststo clear

// Declare outcome variable
global y dv_nongroup_inc

//Decleare kernel
global kernel uniform

//Declare running variable
global run rv
global run_reg rv rv_dem 

// Optimal CCT bandwidth
rdbwselect $y $run , kernel($kernel)
g bw = 1 if rv>- e(h_mserd) & rv<e(h_mserd)
scalar h=e(h_mserd)

// For standard errors clustered 
global cluster state_dist


******************************
* 1) SAMPLE 1978-2004 (sample3)
******************************
	 
		eststo: reg $y dem $run_reg if bw==1 & sample3==1 , cluster($cluster) // D
		estadd scalar obs = e(N)
		corr dem maj if e(sample)
		estadd scalar corr = r(rho) 
		estadd local timefe "No" 
		quietly: sum $y if e(sample) & dem==1
		estadd scalar ydem = r(mean)
		quietly: sum $y if e(sample) & dem==0
		estadd scalar yrep = r(mean)
		estadd scalar d_pchange = 100*(_b[dem])/r(mean)
		quietly: tab demmaj if e(sample), matcell(x)
		estadd scalar ndemyr = 100* x[2,1]/(x[2,1]+x[1,1])
		estadd scalar bw = h
		
		eststo: reg $y dem maj $run_reg if bw==1 & sample3==1 , cluster($cluster) // D M
		estadd scalar obs = e(N)
		corr dem maj if e(sample)
		estadd scalar corr = r(rho) 
		estadd local timefe "No" 
		quietly: sum $y if e(sample) & dem==1
		estadd scalar ydem = r(mean)
		quietly: sum $y if e(sample) & dem==0
		estadd scalar yrep = r(mean)
		estadd scalar d_pchange = 100*(_b[dem])/r(mean)
		quietly: tab demmaj if e(sample), matcell(x)
		estadd scalar ndemyr = 100* x[2,1]/(x[2,1]+x[1,1])		
		estadd scalar bw = h
		
		eststo: reg $y dem maj dyr* $run_reg if bw==1 & sample3==1, cluster($cluster) // D M FE
		estadd scalar obs = e(N)
		corr dem maj if e(sample)
		estadd scalar corr = r(rho) 
		estadd local timefe "Yes" 
		quietly: sum $y if e(sample) & dem==1
		estadd scalar ydem = r(mean)
		quietly: sum $y if e(sample) & dem==0
		estadd scalar yrep = r(mean)
		estadd scalar d_pchange = 100*(_b[dem])/r(mean)
		quietly: tab demmaj if e(sample), matcell(x)
		estadd scalar ndemyr = 100* x[2,1]/(x[2,1]+x[1,1])		
		estadd scalar bw = h
		
		
		
******************************
* 2) SAMPLE DEM 1978-1992 (sample4)
******************************
	 
		eststo: reg $y dem $run_reg if bw==1 & sample4==1 , cluster($cluster) // D
		estadd scalar obs = e(N)
		corr dem maj if e(sample)
		estadd scalar corr = r(rho) 
		estadd local timefe "No" 
		quietly: sum $y if e(sample) & dem==1
		estadd scalar ydem = r(mean)
		quietly: sum $y if e(sample) & dem==0
		estadd scalar yrep = r(mean)
		estadd scalar d_pchange = 100*(_b[dem])/r(mean)
		quietly: tab demmaj if e(sample), matcell(x)
		estadd scalar ndemyr = 100
		estadd scalar bw = h

		
		eststo: reg $y dem  dyr* $run_reg if bw==1 & sample4==1, cluster($cluster) // D M FE
		estadd scalar obs = e(N)
		corr dem maj if e(sample)
		estadd scalar corr = r(rho) 
		estadd local timefe "Yes" 
		quietly: sum $y if e(sample) & dem==1
		estadd scalar ydem = r(mean)
		quietly: sum $y if e(sample) & dem==0
		estadd scalar yrep = r(mean)
		estadd scalar d_pchange = 100*(_b[dem])/r(mean)
		quietly: tab demmaj if e(sample), matcell(x)
		estadd scalar ndemyr = 100
		estadd scalar bw = h
		

******************************
* 3) SAMPLE DEM-SKEWED 1978-1994 (sample1)
******************************
	 
		eststo: reg $y dem $run_reg if bw==1 & sample1==1 , cluster($cluster) // D
		estadd scalar obs = e(N) 
		corr dem maj if e(sample)
		estadd scalar corr = r(rho) 
		estadd local timefe "No" 
		quietly: sum $y if e(sample) & dem==1
		estadd scalar ydem = r(mean)
		quietly: sum $y if e(sample) & dem==0
		estadd scalar yrep = r(mean)
		estadd scalar d_pchange = 100*(_b[dem])/r(mean)
		quietly: tab demmaj if e(sample), matcell(x)
		estadd scalar ndemyr = 100* x[2,1]/(x[2,1]+x[1,1])
		estadd scalar bw = h
		
		eststo: reg $y dem maj $run_reg if bw==1 & sample1==1 , cluster($cluster) // D M
		estadd scalar obs = e(N) 
		corr dem maj if e(sample)
		estadd scalar corr = r(rho) 
		estadd local timefe "No" 
		quietly: sum $y if e(sample) & dem==1
		estadd scalar ydem = r(mean)
		quietly: sum $y if e(sample) & dem==0
		estadd scalar yrep = r(mean)
		estadd scalar d_pchange = 100*(_b[dem])/r(mean)	
		quietly: tab demmaj if e(sample), matcell(x)
		estadd scalar ndemyr = 100* x[2,1]/(x[2,1]+x[1,1])		
		estadd scalar bw = h
		
		eststo: reg $y dem maj dyr* $run_reg if bw==1 & sample1==1, cluster($cluster) // D M FE
		estadd scalar obs = e(N) 
		corr dem maj if e(sample)
		estadd scalar corr = r(rho) 
		estadd local timefe "Yes" 
		quietly: sum $y if e(sample) & dem==1
		estadd scalar ydem = r(mean)
		quietly: sum $y if e(sample) & dem==0
		estadd scalar yrep = r(mean)
		estadd scalar d_pchange = 100*(_b[dem])/r(mean)
		quietly: tab demmaj if e(sample), matcell(x)
		estadd scalar ndemyr = 100* x[2,1]/(x[2,1]+x[1,1])	
		estadd scalar bw = h

******************************
* 4) SAMPLE REP 1994-2004 (sample5)
******************************
	 
		eststo: reg $y dem $run_reg if bw==1 & sample5==1 , cluster($cluster) // D
		estadd scalar obs = e(N) 
		corr dem maj if e(sample)
		estadd scalar corr = r(rho) 
		estadd local timefe "No" 
		quietly: sum $y if e(sample) & dem==1
		estadd scalar ydem = r(mean)
		quietly: sum $y if e(sample) & dem==0
		estadd scalar yrep = r(mean)
		estadd scalar d_pchange = 100*(_b[dem])/r(mean)
		quietly: tab demmaj if e(sample), matcell(x)
		estadd scalar ndemyr = 0
		estadd scalar bw = h
		
		
		eststo: reg $y dem  dyr* $run_reg if bw==1 & sample5==1, cluster($cluster) // D M FE
		estadd scalar obs = e(N) 
		corr dem maj if e(sample)
		estadd scalar corr = r(rho) 
		estadd local timefe "Yes" 
		quietly: sum $y if e(sample) & dem==1
		estadd scalar ydem = r(mean)
		quietly: sum $y if e(sample) & dem==0
		estadd scalar yrep = r(mean)
		estadd scalar d_pchange = 100*(_b[dem])/r(mean)
		quietly: tab demmaj if e(sample), matcell(x)
		estadd scalar ndemyr = 0		
		estadd scalar bw = h

		
******************************
* 5) SAMPLE REP-SKEWED 1992-2004 (sample2)
******************************
	 
		eststo: reg $y dem $run_reg if bw==1 & sample2==1 , cluster($cluster) // D
		estadd scalar obs = e(N) 
		corr dem maj if e(sample)
		estadd scalar corr = r(rho) 
		estadd local timefe "No" 
		quietly: sum $y if e(sample) & dem==1
		estadd scalar ydem = r(mean)
		quietly: sum $y if e(sample) & dem==0
		estadd scalar yrep = r(mean)
		estadd scalar d_pchange = 100*(_b[dem])/r(mean)
		quietly: tab demmaj if e(sample), matcell(x)
		estadd scalar ndemyr = 100* x[2,1]/(x[2,1]+x[1,1])
		estadd scalar bw = h
		
		eststo: reg $y dem maj $run_reg if bw==1 & sample2==1 , cluster($cluster) // D M
		estadd scalar obs = e(N) 
		corr dem maj if e(sample)
		estadd scalar corr = r(rho) 
		estadd local timefe "No" 
		quietly: sum $y if e(sample) & dem==1
		estadd scalar ydem = r(mean)
		quietly: sum $y if e(sample) & dem==0
		estadd scalar yrep = r(mean)
		estadd scalar d_pchange = 100*(_b[dem])/r(mean)
		quietly: tab demmaj if e(sample), matcell(x)
		estadd scalar ndemyr = 100* x[2,1]/(x[2,1]+x[1,1])		
		estadd scalar bw = h
		
		eststo: reg $y dem maj dyr* $run_reg if bw==1 & sample2==1, cluster($cluster) // D M FE
		estadd scalar obs = e(N) 
		corr dem maj if e(sample)
		estadd scalar corr = r(rho) 
		estadd local timefe "Yes" 
		quietly: sum $y if e(sample) & dem==1
		estadd scalar ydem = r(mean)
		quietly: sum $y if e(sample) & dem==0
		estadd scalar yrep = r(mean)
		estadd scalar d_pchange = 100*(_b[dem])/r(mean)
		quietly: tab demmaj if e(sample), matcell(x)
		estadd scalar ndemyr = 100* x[2,1]/(x[2,1]+x[1,1])			
		estadd scalar bw = h
		
	** View table
			#delimit ;
			esttab ,
				 se(3) b(3) nonotes 
				keep(dem maj)
				stats(
					timefe ydem yrep ndemyr corr obs,
					labels(
						"Electoral cycle FE"
						"Mean Y if D=1"
						"Mean Y if D=0"
						"Obs. in dem years (%)"
						"Corr(D,M)"
						"Observations"
						)
					fmt(%3s 0 0 0 2 0)
				)
				star(* 0.10 ** 0.05 *** 0.01)
				replace ;
			#delimit cr	
	
	
	
	
	
	** Latex
			#delimit ;
			esttab  using "results/table3.tex",
				booktabs se(2) b(2) nonotes  nomtitles nostar
				keep(dem maj)
				stats(
					timefe ydem yrep ndemyr corr obs,
					labels(
						"Electoral cycle FE"
						"Mean Y if D=1"
						"Mean Y if D=0"
						"Obs. in dem years (\%)"
						"Corr(D,M)"
						"Observations"
						)
					fmt(%3s 0 0 0 2 0)
				)
				mgroups(
					"1978-2004" "Dem. control: 1978-1992" "1978-1994" "Rep. control: 1994-2004" "1990-2004"
					, pattern(1 0 0 1 0 1 0 0 1 0 1 0 0) 
					prefix(\multicolumn{@span}{c}{) 
					suffix(}) 
					span 
					erepeat(\cmidrule(lr){@span})
				)
				varlabels(
					dem "D"
					maj "M"
					)
				replace ;
			#delimit cr

			
eststo clear

